﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using TinyUtil;

namespace MebAppInstaller
{
    class PackUtil
    {
        /// <summary>
        /// 打包app文件为app.zip，下次新版更新时可使用差分下载减少网络消耗和更新时间
        /// </summary>
        /// <param name="path"></param>
        public static void PackApp(string path)
        {
            var appJsonFile = Path.Combine(path, "app.json");
            if (!File.Exists(appJsonFile))
            {
                Console.WriteLine("file app.json is required");
            }
            else
            {
                var appJson = JsonFile.Read<AppInfo>(appJsonFile);

                var zipFiles = new List<string>();
                foreach (var item in appJson.Files)
                {
                    var fileName = Path.Combine(path, item);
                    zipFiles.Add(fileName);
                }
                zipFiles.Add(appJsonFile);
                ZipHelper.ZipFiles(path, zipFiles.ToArray(), Path.Combine(path, "app.zip"));
                Console.WriteLine("zip ok.");
            }
        }
    }
}
